//
//  DGCoreTextPatternSetting.m
//  DGCoreTextView
//
//  Created by Jinxiao on 4/29/14.
//  Copyright (c) 2014 debugeek. All rights reserved.
//

#import "DGCoreTextPatternSetting.h"

@implementation DGCoreTextPatternSetting

- (NSArray *)preprocessedPatternsForString:(NSString *)string
{
    NSMutableArray *patterns = [NSMutableArray array];

    if(self.predicate.length > 0) {
        NSRegularExpression *regex = [[NSRegularExpression alloc] initWithPattern:self.predicate options:0 error:NULL];
        [regex enumerateMatchesInString:string options:0 range:NSMakeRange(0, string.length) usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop) {
            DGCoreTextPattern *pattern = [[DGCoreTextPattern alloc] init];
            pattern.text = [string substringWithRange:result.range];
            pattern.range = result.range;
            pattern.patternSetting = self;
            [patterns addObject:pattern];
        }];
    }
    
    return patterns;
}

@end
